<template>
  <div class="zhuce">
    <van-nav-bar
      title="注册"
      left-arrow
      @click-left="$router.go(-1)"
      class="bar"
    />
    <div class="form">
      <van-form @submit="onSubmit">
        <van-field
          v-model="phone"
          name="phone"
          label="手机号"
          placeholder="手机号"
          required
          :rules="[
            { required: true, message: '请填写手机号' },
            {
              pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
              message: '手机号格式不对'
            }
          ]"
        />
        <van-field
          v-model="password"
          type="password"
          name="password"
          label="密码"
          placeholder="密码"
          required
          :rules="[
            { required: true, message: '请填写密码' },
            { pattern: /\d{6}/, message: '密码格式不对' }
          ]"
        />
        <van-field
          v-model="username"
          type="username"
          name="nickname"
          label="用户昵称"
          placeholder="请填写昵称"
          required
          :rules="[{ required: true, message: '请填写昵称' }]"
        />
        <van-field
          v-model="code"
          type="code"
          name="captcha"
          label="验证码"
          placeholder="验证码"
          required
          :rules="[{ required: true, message: '请填写验证码' }]"
        />
        <div style="margin: 16px;">
          <van-button
            round
            block
            type="info"
            native-type="submit"
            @click="emitCode"
            >发送验证码</van-button
          >
          <van-button round block type="info" native-type="submit"
            >提交</van-button
          >
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { isCode, zhuceUser } from './user'
export default {
  name: 'zhuce',
  data () {
    return {
      phone: '',
      password: '',
      username: '',
      code: ''
    }
  },
  // async created () {
  //   // console.log(this.phone)
  //   let res = await isCode({ phone: this.phone })
  //   console.log(res)
  // },
  methods: {
    async emitCode () {
      console.log(this.phone)
      let res = await isCode({ phone: this.phone })
      console.log(res)
    },
    async onSubmit (values) {
      console.log(values)
      let res = await zhuceUser(values)
      this.$router.push({ path: '/login' })
      console.log(res)
    }
  }
}
</script>

<style scoped>
.form {
  margin-top: 20px;
}
.bar {
  height: 44px;
  line-height: 44px;
}
.van-button {
  margin-top: 20px;
}
</style>
